package com.galina.notebook.dao.implementation;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import com.galina.notebook.dao.GenericDaoImpl;
import com.galina.notebook.dao.api.ParkingPlaceDaoRemote;
import com.galina.notebook.persistence.ParkingPlace;
import com.galina.notebook.persistence.User;

@Stateless
public class ParkingPlaceDao extends GenericDaoImpl<ParkingPlace> implements ParkingPlaceDaoRemote {

	@Override
	public ParkingPlace create(ParkingPlace t) {
		if (t.getUsers().size() > 0)
		{
			List<User> list = new ArrayList<User>();
			for (User u : t.getUsers()){
				User newUser = em.find(User.class,u.getIdNumber());
				if (newUser == null){
					//list.add(em.persist(u));
					list.add(u);
				}else
				{
					list.add(newUser);
				}
			}
			t.setUsers(list);
		}
		return super.create(t);
	}

}
